package com.kangyi.admin.service.Impl;

import com.kangyi.admin.mapper.AdminUserMapper;
import com.kangyi.admin.mapper.BannerMapper;
import com.kangyi.admin.mapper.UserMapper;
import com.kangyi.admin.service.BannerService;
import com.kangyi.constant.MessageConstant;
import com.kangyi.context.BaseContext;
import com.kangyi.dto.BannerDTO;
import com.kangyi.dto.BulkOperationDTO;
import com.kangyi.dto.EditBannerDTO;
import com.kangyi.entity.AdminUser;
import com.kangyi.entity.Banner;
import com.kangyi.exception.AccountException;
import com.kangyi.exception.DataException;
import com.kangyi.exception.DataNotExistException;
import com.kangyi.exception.PermissionsException;
import com.kangyi.vo.AdminUserVO;
import com.kangyi.vo.BannerListVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Blank
 * @version 1.0
 */
@Service
public class BannerServiceImpl implements BannerService {

    @Autowired
    private BannerMapper bannerMapper;

    @Autowired
    private AdminUserMapper adminUserMapper;

    //调用后检查当前线程id是否有足够的权限进行后续操作
    public void validateUserPermissions() {
        Integer currentId = BaseContext.getCurrentId();
        AdminUserVO adminUserVO = adminUserMapper.selectById(currentId);
        if (!adminUserVO.getPermissions().equals(1)) {
            throw new PermissionsException(MessageConstant.INSUFFICIENT_PRIVILEGE);
        }
    }

    @Override
    public List<BannerListVO> listBanner() {
        validateUserPermissions();
        List<BannerListVO> bannerListVOList = bannerMapper.listBanner();
        if (bannerListVOList == null) {
            throw new DataNotExistException(MessageConstant.DATA_NOT_EXIST);
        }
        return bannerListVOList;
    }

    @Override
    public void addBanner(BannerDTO bannerDTO) {
        validateUserPermissions();
        Banner banner = new Banner();
        BeanUtils.copyProperties(bannerDTO, banner);
        banner.setStatus(1);
        try {
            bannerMapper.addBanner(banner);
        } catch (Exception e) {
            throw new DataException(MessageConstant.DATA_ADD_FAIL);
        }
    }

    @Override
    public void deleteBanner(List<Integer> ids) {
        validateUserPermissions();
        for (Integer id : ids) {
            Banner banner = bannerMapper.getById(id);
            if (banner.getStatus() == 1) {
                throw new DataException(MessageConstant.PLEASE_SET_THE_STATUS_TO_DISABLED);
            }
            bannerMapper.deleteById(id);
        }
    }

    @Override
    public void bulkOperation(BulkOperationDTO bulkOperationDTO) {
        validateUserPermissions();
        for (Integer id : bulkOperationDTO.getIds()) {
            Banner banner = Banner.builder()
                    .id(id)
                    .status(bulkOperationDTO.getType())
                    .build();
            bannerMapper.bulkOperation(banner);
        }
    }

    @Override
    public void editBanner(EditBannerDTO editBannerDTO) {
        validateUserPermissions();
        Integer sort = editBannerDTO.getSort();
        Banner b = bannerMapper.getBySort(sort);
        if (b != null) {
            throw new DataException(MessageConstant.DATA_EDIT_FAIL);
        }
        Banner banner = new Banner();
        BeanUtils.copyProperties(editBannerDTO, banner);
        bannerMapper.editBanner(banner);
    }
}
